package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"testGin/tool"
)

func Authorization() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 获取不到是空字符串，不是nil
		authorization := c.GetHeader("Authorization")
		_, err := tool.ParseToken(authorization)
		if err != nil {
			// 认证过期或者失败，跳转到登录
			c.JSON(http.StatusUnauthorized, gin.H{
				"msg":  "认证失败",
				"code": 401,
			})
			c.Abort()
		}
		c.Next()
	}
}
